import bonetime from 'bone-time'
import com from './addon/com'

var components = com([
	'HeaderLayout',
	'HeaderDock',
	'LayoutMain',
	'LayoutBox',
	'LayoutSideMenu',
	'TabsSide',
	'TabsItemSide',
	'Icon',
	'List',
	'ListItem',
	'Product2',
	'ShoppingDock',
	'Btn'
])

//cvxd
new Vue({
	el: '#app',
	components: components,
	data (){
		return {
			data: [{data:[],show:false},{data:[],show:false},{data:[],show:false},{data:[],show:false},{data:[],show:false},{data:[],show:false},{data:[],show:false}],
			//data: [{data:[]},{data:[]},{data:[]},{data:[]},{data:[]}],
			//data: [],
			request: null,
			total: 0,
			lock: false,
			types: {
				1: '早餐',
				2: '中餐',
				3: '晚餐'
			}
		}
	},
	created: function(){
		var haha = ['一', '二', '三', '四', '五', '六', '日'];
		var priceall = 0;

		//
		for(var i=0; i<DATE.length; i++){
			//var day = new Date(DATE[i]).getDay();
			var datee = new bonetime(DATE[i]);
			this.data[i].show = true;
			this.data[i].date = '周' + haha[datee.obj.getDay()-1] + '(' + datee.format('MM月dd日') + ')';

			for(var n=0; n<PRODUCTS.length; n++){
				if(PRODUCTS[n].date == DATE[i]){
					PRODUCTS[n].name += '('+this.types[PRODUCTS[n].type]+')'
					this.data[i].data.push(PRODUCTS[n]);
					priceall += PRODUCTS[n].price*PRODUCTS[n].amount;
				}
			}
		}

		this.total = PRODUCTS.length;


		for(var i = 0; i<this.data.length; i++){
			if(this.data[i].data.length == 0){
				this.data[i].empty = true;
			} else {
				this.data[i].empty = false;
			}
		}

	},
	events: {

	},
	methods: {
		pay: function(){
			var _that = this
			if(this.lock) return
			/*
			if(this.total < 2*DATE.length){
				bone.dialog.init('速派友情提醒', '您还未选满本周的商品，请在全部选购完成后提交订单').ok(function(){
								return true;
							});

				return;
			}*/

			this.lock = true
			$.ajax({
				url: SITE_URL + '/order/save/' + $('#code').val(),
				type: 'post',
				success: function(data){
					_that.loading = false
					bone.dialog.init('速派友情提醒', data.msg).ok(function(){
						if(!data.code){

						} else {

							window.location = SITE_URL + '/order/detail/id/' + data.data;
						}
								return true;
							});


				},
				error: function(){
					alert('网络发生错误，请重试');
				}
			});
		},
		clean: function(){

			bone.dialog.init('速派友情提醒', '确定清空购物车？',{
				buttons: 'double'
			}).ok(function(){
				$.ajax({
					url: SITE_URL + '/shoppingcart/clear',
					type: 'get',
					success: function(data){
						if(data.code){
							window.location.reload();
						} else {
							alert(data.msg)
						}

					},
					error: function(){
						//alert('网络错误，请重试');
					}

				});
				return true;
			}).no(function(){
				return true;
			});


		},
		delete: function(pra){
			this.request = $.ajax({
				url: SITE_URL + '/shoppingcart/delete',
				data: {
					id: pra.id
				},
				type: 'post',
				success: function(data){
					if(data.code){
						window.location.reload();
					} else {
						bone.dialog.init('速派友情提醒', data.msg).ok(function(){
								return true;
							});
					}

				},
				error: function(){
					//alert('网络错误，请重试');
				}

			});
			return;
		},
		buy: function(pra){
			if(this.request){
				this.request.abort();
			}
			var _this = this;

			if(pra.counter.val == 0){

				this.request = $.ajax({
					url: SITE_URL + '/shoppingcart/delete',
					data: {
						id: pra.id
					},
					type: 'post',
					success: function(data){
						if(data.code){
							window.location.reload();
						} else {
							bone.dialog.init('速派友情提醒', data.msg).ok(function(){
								return true;
							});
						}

					},
					error: function(){
						//alert('网络错误，请重试');
					}

				});
				return;
			}

			this.request = $.ajax({
				url: SITE_URL + '/shoppingcart/updatecart',
				data: {
					id: pra.id,
					amount: pra.counter.val
				},
				type: 'post',
				success: function(data){
					if(data.code){
						_this.total = data.data;
					} else {
						bone.dialog.init('速派友情提醒', data.msg).ok(function(){
								return true;
							});
					}

				},
				error: function(){
					//alert('网络错误，请重试');
				}

			});
		}
	}
})
